package com.doit.day01;

import lombok.*;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;


/**
 * 1.打开文件
 * 2.读取数据
 * 3.判断数据是否为null，如果是null，代表没有数据了，返回false，否则返回true
 * 4.将读取过来的数据进行逻辑处理==》将一行数据转换成Hero对象返回给用户
 */
public class MyIterator implements Iterator<Hero> {
    BufferedReader br = null;
    String line = "";

    public MyIterator() throws Exception {
        //打开文件
        br = new BufferedReader(new FileReader("D:\\develop\\ideaWorkSpace\\myself\\Teach\\scalaDemo\\data\\hero"));

    }
    /**
     * 判断还有没有数据
     * @return
     */
    @Override
    public boolean hasNext() {
        boolean  flag= false;
        try {
            //读取到了这一行数据
            line = br.readLine();
            //判断他是否为null
            flag = line != null;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return flag;
    }
    /**
     * 获取数据返回给用户
     * @return
     */
    @Override
    public Hero next() {
        //拿到一行数据去处理
        ////6_大郎:8,150
        //大郎:8,150
        String[] arr1 = line.split("_");
        //8,150
        String[] arr2 = arr1[1].split(":");

        String[] arr3 = arr2[1].split(",");

        return new Hero(Integer.parseInt(arr1[0]),arr2[0],Integer.parseInt(arr3[0]),Integer.parseInt(arr3[1]));
    }
}


@AllArgsConstructor
@NoArgsConstructor
@Setter
@Getter
@ToString
class Hero implements Comparable<Hero>{
    //6_大郎:8,150
    private int id;
    private String name;
    private int age;
    private int power;

    @Override
    public int compareTo(Hero o) {
        return this.name.compareTo(o.name);
    }
}
